<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>v-for</title>
    <script type="text/javascript" src="../assets/js/vue-2.3.0.js"></script>
</head>
<body>
    <h1>v-for</h1>
    <hr>
    <div id="app">
        <ul>
            <!-- <li v-for="item in items">{{item}}</li> -->
            <li v-for="item in sortItems">{{item}}</li>            
        </ul>
        <ul>
            <li v-for="(item,index) of sortStudent" >{{index+1}}:{{item.name}}-{{item.age}}</li>
        </ul>
        
    </div>
</body>
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            items: [52, 36, 93, 66, 8, 12, 77, 56, 32],
            students: [
                { name: 'jspang', age: 32 },
                { name: 'Panda', age: 30 },
                { name: 'PanPaN', age: 21 },
                { name: 'King', age: 45 }
            ]
        },
        computed: {
            sortItems: function(){
                return this.items.sort(sortNumber)
            },
            sortStudent: function(){
                return sortByKey(this.students,'age');
            }
        }
    })
    function sortNumber(a,b){
            return a-b
    }
    //数组对象方法排序:
    function sortByKey(array,key){
        return array.sort(function(a,b){
        var x=a[key];
        var y=b[key];
        return ((x<y)?-1:((x>y)?1:0));
    });
    }
</script>
</html>